package cn.tradewin.advisor.pages.account;

import java.util.List;

import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.apache.shiro.subject.Subject;
import org.apache.tapestry5.Link;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.services.BeanModelSource;
import org.apache.tapestry5.services.PageRenderLinkSource;
import org.slf4j.Logger;
import org.tynamo.security.services.SecurityService;

import cn.tradewin.advisor.exceptions.DBException;
import cn.tradewin.advisor.persist.model.Roles;
import cn.tradewin.advisor.persist.services.AdvisorSecurityService;

public class Inbox {
	
	@Inject
	private Logger log;
	
	@Inject
	private PageRenderLinkSource resource;
	
	@Inject
	private BeanModelSource beanModelSource;
	
	@Property
	private BeanModel<Task> beanModel;
	
	@Property
	private List<Task> assignedTasks;
	
	@Property
	private Task assignedTask;
	
	@Property
	private List<Task> candidateTasks;
	
	@Property
	private Task candidateTask;
	
	@Inject
	private AdvisorSecurityService advisorSecurityService;
	
	@Inject
	private SecurityService securityService;
	
	@Inject
	private TaskService taskService;
	
	@Inject
	private Messages messages;
	
	void setupRender() throws DBException {
		beanModel = beanModelSource.createDisplayModel(Task.class, messages);
		beanModel.add("action", null);
		beanModel.include("name", "description", "createtime","action");
		
		// get login account information about userid and roles
		Subject subject = securityService.getSubject();
		String userid = (String)subject.getPrincipal();
		List<Roles> roles = advisorSecurityService.getAssignedRolesForAccount(userid);
		
		// tasks assigned to login user
		assignedTasks = taskService.createTaskQuery().taskAssignee(userid).list();
		
		// candidate tasks of login user's roles. 
		List<String> roleNames = CollectionFactory.newList();
		for (Roles role : roles) {
			roleNames.add(role.getRoleName());
		}
		candidateTasks = taskService.createTaskQuery().taskCandidateGroupIn(roleNames).list();
	}
	
	public boolean isEmptyAssigned() {
		return assignedTasks == null || assignedTasks.size() == 0;
	}

	public boolean isEmptyCandidate() {
		return candidateTasks == null || candidateTasks.size() == 0;
	}
	
	/**
	 * claiming the candidate task
	 * @param taskId
	 */
	void onClaim(String taskId) {
		Subject subject = securityService.getSubject();
		String userId = (String)subject.getPrincipal();
		taskService.claim(taskId, userId);
	}
	
	/**
	 * based on task type, move to specified page to deal with the task.
	 * @param taskId : task instance id stored in the database.
	 * @return deal page based on task definition key.
	 */
	Link onDeal(String taskId) {
		// prefix page name of task deal page.
		String pageName = "tasks/Deal";
		// get the task definition key
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		pageName += task.getTaskDefinitionKey();
		return resource.createPageRenderLinkWithContext(pageName, taskId);
	}
}